package com.nondo.rdp.finance.endpoint.api.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.RoleUtils;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.product.SecurityOpen;
import com.nondo.rdp.finance.service.product.SecurityOpenService;

@RestController("apiAdminSecurityOpen")
@RequestMapping("/api/admin/securityOpen")
public class SecurityOpenEndpoint {
    @Autowired
    SecurityOpenService securityOpenService;
    
    /**
     * 查看证券开户
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
	public View info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<SecurityOpen> view = new View<SecurityOpen>(20000, "获取数据成功");
		
		SecurityOpen securityOpen = null;
		if (id != null) {
			securityOpen = securityOpenService.findOne(id);
		} else {
			securityOpen = new SecurityOpen();
		}

		view.setData(securityOpen);
		return view;
	}
    
    /**
     * 添加证券开户
     * @param request
     * @param response
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/add")
	public View add(HttpServletRequest request, HttpServletResponse response,
			@Validated @RequestBody SecurityOpen securityOpen, BindingResult result) {
		View view = new View(20000, "添加成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			this.securityOpenService.save(securityOpen);
		}
		return view;
	}

    /**
     * 更新证券开户
     * @param request
     * @param response
     * @param id
     * @return
     */
	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/update")
	public View update(HttpServletRequest request,
			HttpServletResponse response,
			@Validated @RequestBody SecurityOpen securityOpen, BindingResult result) {
		View view = new View(20000, "更新成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			SecurityOpen securityOpenDb = securityOpenService.findOne(securityOpen.getId());
			BeanUtils.copy(securityOpen, securityOpenDb);
			this.securityOpenService.update(securityOpenDb);
		}

		return view;
	}

    /**
     * 获取证券开户列表
     * @param request
     * @param response
     * @param query
     * @return
     */
	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
    @RequestMapping("/list")
    public View<Page> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<SecurityOpen> query) {
        View<Page> view = new View(20000, "获取列表成功");

        SecurityOpen securityOpen = query.getData();

        Page<SecurityOpen> page = securityOpenService.findAll(new Page<SecurityOpen>(query.getPage(), query.getSize()), securityOpen);

        view.setData(page);

        return view;
    }

    /**
     * 删除证券开户
     * @param request
     * @param response
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "删除成功");

        this.securityOpenService.delete(id);

        return view;
    }
}